<?php
/**
 *
 *
 *
 */
namespace Yz\modules\Generic;

class Action_SelectFilterList
    extends Action_SelectList
{

    /*  Define constants for filter names:
     *  const FILTER_TEST=1
     *  const FILTER_EVEN_ID=2
     */

    /** The (named) list of SQL subclauses which can be applied as filters in WHERE
     *  @var array
     */
    protected $filters = array(
        /*  self::FILTER_EVEN_ID => 'id%2=0'
         *  self::FILTER_TEST => "somefield LIKE '\$test%'"
         */
        );

    /** The list of filters applied by caller
     *  @var array
     */
    protected $filtersApplied = array();



    /************************************
     *          Public methods          *
     ************************************/

    /**
     *  @param string|const filterName
     *  @return chainable
     */
    public function applyFilter($filterName)
    {
        $filterName = \Yz_String::testNotEmpty($filterName);
        if (! array_key_exists($filterName, $this->filters)) {
            throw new \Yz_Exception("Unknown filter '{$filterName}'");
        }
        $this->filtersApplied[] = $filterName;
        return $this;
    }


    /************************************
     *       Protected methods          *
     ************************************/

    /**
     *  Modified SELECT statement creator
     *
     */
    protected function buildSelect()
    {
        debug();
        $sql = parent::buildSelect();
        foreach ($this->filtersApplied as $filterName) {
            $sql
                ->where($this->filters[$filterName]);
        }
        return $sql;
    }



}
